import request from '@/utils/request'
import Base64 from "../utils/base64";
import {updateSession} from "./userInfo";
import store from '@/store'

// 获取滑块验证码拼图位置
export function getCodePosition(puzzleBaseSize) {
  return request({
    url: '/getCodePosition',
    method: 'get',
    params: {puzzleBaseSize: puzzleBaseSize}
  })
}

// 登录方法
export function login(mobile, password, smscode, type) {
  const data = {
    mobile,
    password,
    smscode,
    type
  }
  return request({
    'url': '/login',
    headers: {
      isToken: false
    },
    'method': 'post',
    'data': data
  })
}

// 获取用户详细信息
export function getInfo() {
  return request({
    'url': '/getInfo',
    'method': 'get'
  })
}

// 退出方法
export function logout() {
  return request({
    'url': '/logout',
    'method': 'post'
  })
}

// 获取短信验证码
export function getsmsCode(mobile, type) {
  let codeToken =  uni.getStorageSync("c");
  // 每次令牌只能使用一次
  uni.removeStorageSync("c");
  return request({
    'url': '/register/smsCode',
    method: 'get',
    header: {
      codeToken: codeToken
    },
    params: {mobile: mobile, smsType: type}
  })
}

// 获取验证码
export function getCodeImg() {
  return request({
    'url': '/captchaImage',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}

// 切换用户
export function changeUser(companyId) {
  return request({
    url: '/changeUser',
    method: 'get',
    headers: {
      osType:2,// 0pc   1微信小程序  2 h5
    },
    params: { companyId: companyId }
  })
}

// 切换用户 仅切换用户表staffCompanyID
export function changeStaffCompanyId(companyId, mobile) {
  return request({
    url: '/changeStaffCompanyId',
    method: 'get',
    params: {companyId: Base64.encode(companyId).split("").reverse().join(""), mobile: Base64.encode(mobile).split("").reverse().join("")}
  })
}

// 切换工作台 控制台
export function changeWork(menuBelong) {
  return request({
    url: '/changeWork',
    method: 'get',
    params: { menuBelong: menuBelong }
  })
}

// 获取用户协议
export function registerAgreement() {
  return request({
    url: '/register/registerAgreement',
    method: 'get'
  })
}

// 励氪签隐私政策
export function privacyPolicy() {
  return request({
    url: '/register/privacyPolicy',
    method: 'get'
  })
}

// 电子签约服务购买协议
export function eleBuyAgreement() {
  return request({
    url: '/register/eleBuyAgreement',
    method: 'get'
  })
}

// 电子签约服务购买协议
export function getAgreement(id) {
  return request({
    url: '/register/getAgreement/' + id,
    method: 'get'
  })
}

// 通用解密接口
export async function DecryptCommon(data) {
  return await request({
    url: '/common/decrypt/' + data,
    method: 'get',
  })
}

// 通用加密接口
export async function EncryptCommon(data) {
  return await request({
    url: '/common/encrypt/',
    method: 'get',
    params: {data:data}
  })
}

// 注册方法
export function register(data) {
  return request({
    url: '/register/create',
    headers: {
      isToken: false
    },
    method: 'post',
    data: data
  })
}

// 修改登录密码
export function resetPassWord(data) {
  return request({
    url: '/register/resetPassWord',
    method: 'post',
    data: data
  })
}

//找回密码手机验证码
export function checkSmsCode(params) {
  return request({
    url: '/register/checkSmsCode',
    headers: {
      isToken: false
    },
    method: 'get',
    params: params
  })
}

// 找回密码 
export function backPassWordByMobile(data) {
  return request({
    url: '/register/backPassWordByMobile',
    headers: {
      isToken: false
    },
    method: 'get',
    params: data
  })
}

// 个人查找账号
export function backStaff(data) {
  return request({
    url: '/register/backStaff',
    headers: {
      isToken: false
    },
    method: 'get',
    params: data
  })
}

// 企业查找账号
export function backCompany(data) {
  return request({
    url: '/register/backCompany',
    headers: {
      isToken: false
    },
    method: 'get',
    params: data
  })
}

/**
 * 登录之前检查是否需要自动切换企业
 * @param companyId 需要切换的企业id
 * @param content
 * @param confirmText 小程序只支持4个字  '确认'
 * @param url
 */
export function changeCompany(companyId, content, confirmText, url) {
  uni.showModal({
    title: '提示',
    content: content,
    confirmText: confirmText,
    showCancel: false,
    success: (res) => {
      if (res.confirm) {
        changeUser(companyId).then(() => {
          updateSession().then((response) => {
            store.dispatch('GetInfo').then((res) => {
              uni.reLaunch({url: url})
            });
          });
        });
      } else if (res.cancel) {
        console.log('用户点击取消');
      }
    }
  });
}